﻿//using AutoMapper;
//using Hospital.Model.DTO;
//using Hospital.Utility.ApiResult;
//using Microsoft.AspNetCore.Authorization;
//using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Mvc;
//using Hospital.IService;
//using Hospital.Model;
//using SqlSugar;

//namespace Hospital.WebApi.Controllers
//{
//    [Route("api/[controller]")]
//    [ApiController]
   //[Authorize]
//    //将[Authorize]属性放置在控制器类的上方，表示该控制器下的所有动作方法都需要用户进行身份验证,否则会返回401。
//    public class BlogNewsController : ControllerBase
//    {
//        private readonly IBlogNewsService iBlogNewsService;
//        public BlogNewsController(IBlogNewsService iBlogNewsService)
//        {
//            this.iBlogNewsService = iBlogNewsService;
//        }
//        [HttpGet("BlogNews")]
//        public async Task<ActionResult<ApiResult>> GetBlogNews()
//        {
//            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
//            var data = await iBlogNewsService.QueryAsync(c => c.AuthorId == id);
//            if (data == null) return ApiResultHelper.Error("没有更多数据");
//            return ApiResultHelper.Success(data);
//        }
//        [HttpPost("Add")]
//        public async Task<ActionResult<ApiResult>> AddBlogNews(string title, string content, int typeid)
//        {
//            BlogNews blogNews = new BlogNews()
//            {
//                Title = title,
//                Content = content,
//                CreateTime = DateTime.Now,
//                BrowseCount = 0,
//                LikeCount = 0,
//                TypeId = typeid,
//                AuthorId = Convert.ToInt32(this.User.FindFirst("Id").Value)
//            };
//            bool b = await iBlogNewsService.CreateAsync(blogNews);
//            if (!b) return ApiResultHelper.Error("添加失败，服务器出现错误");
//            return ApiResultHelper.Success(blogNews);
//        }
//        [HttpDelete("Delete")]
//        public async Task<ActionResult<ApiResult>> DeleteBlogNews(int id)
//        {
//            bool b = await iBlogNewsService.DeleteAsync(id);
//            if (!b) return ApiResultHelper.Error("删除失败");
//            return ApiResultHelper.Success(b);
//        }
//        /// <summary>
//        /// 
//        /// </summary>
//        /// <param name="id"></param>
//        /// <param name="title"></param>
//        /// <param name="content"></param>
//        /// <param name="typeid"></param>
//        /// <returns></returns>
//        [HttpPut("Edit")]
//        public async Task<ActionResult<ApiResult>> EditBlogNews(int id, string title, string content, int typeid)
//        {
//            BlogNews blogNews = await iBlogNewsService.FindAsync(id);
//            if (blogNews == null) return ApiResultHelper.Error("未找到该文章");
//            blogNews.Title = title;
//            blogNews.Content = content;
//            blogNews.TypeId = typeid;
//            bool b = await iBlogNewsService.EditAsync(blogNews);
//            if (!b) return ApiResultHelper.Error("修改失败");
//            return ApiResultHelper.Success(blogNews);
//        }

//        [HttpGet("GetBlogNewsPage")]
//        public async Task<ApiResult> GetBlogNewsPage([FromServices] IMapper imapper, int page, int size)
//        {
//            RefAsync<int> total = 0; //RefAsync是ref的异步(ref是引用类型)
//            var blogNews = await iBlogNewsService.QueryAsync(page, size, total);
//            try
//            {
//                var blogNewsDTO = imapper.Map<List<BlogNewsDTO>>(blogNews);
//                return ApiResultHelper.Success(blogNewsDTO, total);

//            }
//            catch (Exception ex)
//            {
//                return ApiResultHelper.Error("映射错误");
//            }
//        }
//    }
//}
